package com.ansion.commonchat.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.ansion.string.StringUtils;
import com.ansion.utils.UiUtils;
import com.ansion.commonchat.R;
import com.ansion.utils.LanguageSettingHelper;

import java.util.Collection;
import java.util.List;

public class AlphabetView extends View {
	public static final String SEARCH_KEY = "$";
	private OnTouchingLetterChangedListener m_onTouchingLetterChangedListener;
	private char[] m_keys;
	private int m_choose = -1;
	private Paint m_paint = new Paint();
	private boolean m_showBkg = false;
	private Context m_context;
	private final int MAX_WIDTH = (int) UiUtils.dip2px(23);
	private final int MIN_WIDTH = 7;
	private boolean isArab = false;
	private boolean inited = false;
	public AlphabetView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		if (isInEditMode()) {
			return;
		}
		m_context = context;
	}

	public AlphabetView(Context context, AttributeSet attrs) {
		super(context, attrs);
		if (isInEditMode()) {
			return;
		}
		m_context = context;
	}

	public AlphabetView(Context context) {
		super(context);
		if (isInEditMode()) {
			return;
		}
		m_context = context;
	}

	private void init() {
		if (!"ar".equals(LanguageSettingHelper.getInstance().getAppLanguage())) {
			if(!arFind&&!alphaFind){
				m_keys = sortKeysforEn;
			}else {
				m_keys = sortKeysforEn1;
			}
		} else {
			if(!arFind&&!alphaFind){
				m_keys = sortKeysforAr;
			}else {
				m_keys = sortKeysforArab1;
			}
			isArab = true;
		}

		if (arFind && !alphaFind) {
			m_keys = sortKeysforAr;
		}

		if (alphaFind && !arFind) {
			m_keys = sortKeysforEn;
		}
		inited = true;
		invalidate();
	}

	public void reset() {
		m_context = null;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (!inited) {
			return;
		}
		if (m_context == null)
			return;

		if (m_showBkg) {
			canvas.drawColor(Color.parseColor("#40000000"));
		}

		int len = m_keys.length;
		if (len < 1) {
			return;
		}

		int height = getDrawHeight();
		int width = getMeasuredWidth();
		int singleHeight = height / len;
		int textSize = singleHeight/2;
		if (textSize < MIN_WIDTH)
			textSize = MIN_WIDTH;
		if (textSize > MAX_WIDTH)
			textSize = MAX_WIDTH;

		for (int i = 0; i < len; i++) {
			String s = String.valueOf(m_keys[i]);
			m_paint.setTextSize(textSize);
			m_paint.setColor(Color.parseColor("#555555"));
			m_paint.setTypeface(Typeface.DEFAULT_BOLD);
			m_paint.setAntiAlias(true);
			m_paint.setFakeBoldText(true);
			if (i == m_choose) {
				m_paint.setColor(Color.parseColor("#555555"));
//				m_paint.setFakeBoldText(true);
			}

			float xPos = width / 2 - m_paint.measureText(s) / 2;
			float yPos = singleHeight * i + singleHeight;
			canvas.drawText(s, xPos, yPos, m_paint);
			m_paint.reset();
		}
	}

	private int getDrawHeight(){
		return getMeasuredHeight()- UiUtils.dp(10);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final int oldChoose = m_choose;
		final OnTouchingLetterChangedListener listener = m_onTouchingLetterChangedListener;
		if (m_keys==null){
			m_keys = new char[]{' '};
		}
		int c = (int) (y / getDrawHeight() * m_keys.length);
		if (c >= m_keys.length) {
			c = m_keys.length - 1;
		}
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			m_showBkg = true;
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < m_keys.length) {
					listener.onTouchingLetterChanged(String.valueOf(m_keys[c]));
					m_choose = c;
					resetKeyList(c);
				}
			}
			invalidate();

			break;
		case MotionEvent.ACTION_MOVE:
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < m_keys.length) {
					listener.onTouchingLetterChanged(String.valueOf(m_keys[c]));
					m_choose = c;
					resetKeyList(c);
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			m_showBkg = false;
			m_choose = -1;
			if (null != listener) {
				listener.onTouchingLetterChanged("");
			}
			invalidate();
			break;

		case MotionEvent.ACTION_CANCEL:
			m_showBkg = false;
			m_choose = -1;
			if (null != listener) {
				listener.onTouchingLetterChanged("");
			}
			invalidate();
			break;

		}
		return true;
	}

	private void resetKeyList(int c){
		if (arFind && alphaFind) {
			if (isArab) {// special handle for arab
				if (m_keys == sortKeysforArab1 && c == sortKeysforArab1.length - 1) {
					m_keys = sortKeysforArab2;
					c = sortKeysforArab2.length - 1;
					m_choose = c;
					invalidate();
					startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.tran_bottom_in));
				} else if (m_keys == sortKeysforArab2 && c <= 1) {
					m_keys = sortKeysforArab1;
					invalidate();
					startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.tran_top_in));
				}
			} else {
				if (m_keys == sortKeysforEn1 && c == sortKeysforEn1.length - 1) {
					m_keys = sortKeysforEn2;
					c = sortKeysforEn2.length - 1;
					m_choose = c;
					invalidate();
					startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.tran_bottom_in));
				} else if (m_keys == sortKeysforEn2 && c <= 1) {
					m_keys = sortKeysforEn1;
					invalidate();
					startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.tran_top_in));
				}
			}
		}
	}
	public void setOnTouchingLetterChangedListener(
			OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
		this.m_onTouchingLetterChangedListener = onTouchingLetterChangedListener;
	}

	public interface OnTouchingLetterChangedListener {
		public void onTouchingLetterChanged(String s);
	}

	/**
	 * 仅阿拉伯语
	 */
	private char[] sortKeysforAr = new char[] { '#', 'ا', 'ﺏ', 'ﺕ', 'ﺙ',
			'ﺝ', 'ﺡ', 'ﺥ', 'ﺩ', 'ﺫ', 'ﺭ', 'ﺯ', 'ﺱ', 'ش', 'ص', 'ﺽ', 'ط', 'ظ',
			'ع', 'غ', 'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ي'};
	/**
	 * 仅英语
	 */
	private char[] sortKeysforEn = new char[] { '#', 'A', 'B', 'C',
			'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
			'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
	/**
	 * 混排，系统语言阿拉伯语
	 */
	private char[] sortKeysforArab1 = new char[] { '#', 'ا', 'ﺏ', 'ﺕ', 'ﺙ',
			'ﺝ', 'ﺡ', 'ﺥ', 'ﺩ', 'ﺫ', 'ﺭ', 'ﺯ', 'ﺱ', 'ش', 'ص', 'ﺽ', 'ط', 'ظ',
			'ع', 'غ', 'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ي', 'Z' };
	/**
	 * 混排，系统语言阿拉伯语
	 */
	private char[] sortKeysforArab2 = new char[] { '#', 'ا', 'A', 'B', 'C',
			'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
			'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

	/**
	 * 混排，系统语言非阿拉伯语
	 */
	private char[] sortKeysforEn1 = new char[] { '#', 'A', 'B', 'C',
			'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
			'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' , 'ي'};

	/**
	 * 混排，系统语言非阿拉伯语
	 */
	private char[] sortKeysforEn2 = new char[] { '#', 'A', 'ا', 'ﺏ', 'ﺕ', 'ﺙ',
			'ﺝ', 'ﺡ', 'ﺥ', 'ﺩ', 'ﺫ', 'ﺭ', 'ﺯ', 'ﺱ', 'ش', 'ص', 'ﺽ', 'ط', 'ظ',
			'ع', 'غ', 'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ي'};


	public void setKeys(List<String> sortKeys) {

	}

	public void setKeys(Collection<String> sortedKeys) {

	}

	private boolean arFind = false;
	private boolean alphaFind = false;

	public void checkKeys(List<String> keys) {
		boolean arFind = false;
		boolean alphaFind = false;
		for (String key : keys) {
			if (key == null || key.length() == 0) {
				continue;
			}
			if (StringUtils.isAlpha(key.charAt(0))) {
				alphaFind = true;
				continue;
			}

			if (StringUtils.isArabChar(key.charAt(0))) {
				arFind = true;
				continue;
			}
			if (arFind && alphaFind) {
				break;
			}
		}
		if (inited && this.arFind == arFind && this.alphaFind == alphaFind) {
			return;
		}
		this.arFind = arFind;
		this.alphaFind = alphaFind;
		init();
	}
}
